import Vue from 'vue';
import axios from 'axios';

axios.defaults.baseURL = '/api';

axios.interceptors.request.use((config) => {
    let token = window.sessionStorage.getItem('token');
    if (token) config.headers.token = token;
    return config;
});

axios.interceptors.response.use(
    (response) => {
        let res = response.data;
        if (res.code === 401) {
            window.location.href = '/login';
        }
        return res;
    },
    (error) => {
        return {
            code: 500,
            message: error.message,
        };
    },
);

Vue.prototype.$http = axios;
